<script>
    const PENDING = 'pending'
    const FULFILLED = 'fulfilled'
    const REJECTED = 'rejected'
    class Promise {
        constructor(handle) {
            const isFunction = variable => typeof variable === 'function';
            //定义三个状态
            this._status = PENDING
            this._value = undefined

            if (!isFunction(handle)) {
                throw new Error('MyPromise must accept a function as a parameter')
            }

            try {
                handle(this._resolve, this._reject)
            } catch (e) {
                this._rejected(e)
            }

            //成功队列
            this.fulfilledQueue = []
            //失败队列
            this.rejectedQueue = []
        }
        _resolve(value) {
            if (this._status !== 'pending') return
            this._status = FULFILLED
            this._value = value
        }
        _rejected(value) {
            if (this._status !== 'pending') return
            this._status = REJECTED
            this._value = value
        }

    }
    // let p = new Promise(function (resolve, reject) {
    //     setTimeout(function () {
    //         resolve('success')
    //     }, 2000)
    // })
    // let p2 = p.then('134')
    // p2.then(function (res) { 
    //     console.log(res);
    // })
</script>